import axios from 'axios'
import envconfig from '@/envconfig/envconfig'

export default class Server{
  axios(method,url,params){
    return new Promise((resolve,reject) =>{
      if(typeof params != 'object'){
        params = {}
      }
      let _option = {
        method,
        url,
        baseURL:envconfig.baseURL,
        timeout:30000,
        params:null,
        data:null,
        headers:null,
        withCredentials:true,// send request with Cookie
        validateStatus:(status) =>{
          return status >=200 && status <300
        },
        ...params
      }
      axios.request(_option).then(res =>{
        resolve( typeof res === 'object' ? res.data:JSON.parse(res.data))
      },error => {
        if(error.response){
          reject(error.response.data)
        }else{
          reject(error)
        }
      })
    })
  }
}

